﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using System.IO;
using University.Workshop.Business;
using University.Workshop.Presenters;
using University.Workshop.Presenters.Views;

namespace University.Workshop.WinApp
{
    public partial class ModifyBuyProduct : Form,IProductView
    {
        private OpenFileDialog openFile = new OpenFileDialog();
        ProductPresenter presenter;
        bool valid= false;
        public ModifyBuyProduct(Product product, ProductBuy buy)
        {
            InitializeComponent();
            presenter = new ProductPresenter(this);
            LoadModifyBuyProduct(product, buy);
        }

        private void LoadModifyBuyProduct(Product product, ProductBuy buy)
        {
            textBoxCodeProduct.Text = Convert.ToString(product.IdProduct);
            textBoxDateBuyProduct.Text = buy.DateBuy;
            textBoxIdBuyProduct.Text = Convert.ToString(buy.IdBuyProduct);
            comboBoxGroup.Text = product.GroupProduct;
            textBoxNameProduct.Text = product.NameProduct;
            textBoxStockCurrent.Text = Convert.ToString(product.StockCurrent);
            textBoxSupplier.Text = buy.Supplier;
            textBoxStockOrder.Text = Convert.ToString(buy.StockOrder);
            textBoxUnitPrice.Text = Convert.ToString(buy.StockOrder);
            //textBoxTotalPrice.Text = Convert.ToString(buy.TotalPrice);
            textBoxDescription.Text = buy.Description;
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", buy.ImageBuy)));
                pictureBoxProduct.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxProduct.Image = picture;
            } 
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (ValidateFields.ValidateNumberFields() && !string.IsNullOrEmpty(comboBoxGroup.Text) && valid == true)
            {
                if (presenter.ModifyRegistrationProduct())
                {
                    try
                    {
                        File.Copy(openFile.FileName, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", openFile.SafeFileName)));

                    }
                    catch (Exception)
                    {

                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("error datos incorrectos");
                }
            }
            else
            {
                valid = false;
                MessageBox.Show("All fields are required");
            }
        }

        private void textBoxNameProduct_TextChanged(object sender, EventArgs e)
        {
            errorProviderModifyBuy.SetError(textBoxNameProduct, ValidateFields.NameValidation(textBoxNameProduct.Text));
        }

        private void textBoxSupplier_TextChanged(object sender, EventArgs e)
        {
            errorProviderModifyBuy.SetError(textBoxSupplier, ValidateFields.NameValidation(textBoxSupplier.Text));
        }

        private void buttonTotal_Click(object sender, EventArgs e)
        {
            textBoxTotalPrice.Text = ProductManager.TotalPriceBuy(textBoxUnitPrice.Text, textBoxStockOrder.Text);
            valid = true;
        }

        private void textBoxStockOrder_TextChanged(object sender, EventArgs e)
        {
            errorProviderModifyBuy.SetError(textBoxStockOrder, ValidateFields.NumbersInt(textBoxStockOrder.Text));
        }

        private void textBoxUnitPrice_TextChanged(object sender, EventArgs e)
        {
            errorProviderModifyBuy.SetError(textBoxUnitPrice, ValidateFields.NumbersFloat(textBoxUnitPrice.Text));
        }

        private void buttonLoadPicture_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBoxProduct.ImageLocation = openFile.FileName;

            }
            else
            {
                pictureBoxProduct.Image = null;
            }
        }

        private void comboBoxSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSupplier.Text == "Others")
            {
                textBoxSupplier.Text = "";
            }
            else
            {
                textBoxSupplier.Text = comboBoxSupplier.Text;                
            }
        }
        public int NBuyProduct
        {
            get
            {
                return int.Parse(textBoxIdBuyProduct.Text);
            }
            set
            {
                textBoxIdBuyProduct.Text = Convert.ToString(value);
            }
        }

        public string CIUser
        {
            get { return "6600123"; }
        }

        public string DateBuy
        {
            get { return textBoxDateBuyProduct.Text; }
        }

        public string GroupProduct
        {
            get { return comboBoxGroup.Text; }
        }

        public string NameProduct
        {
            get
            {
                return textBoxNameProduct.Text;
            }
        }

        public string Supplier
        {
            get
            {
                return textBoxSupplier.Text;
            }
        }
        public int StockOrder
        {
            get { return int.Parse(textBoxStockOrder.Text); }
        }

        public float UnitPrice
        {
            get { return float.Parse(textBoxUnitPrice.Text); }
        }

        public float TotalPrice
        {
            get { return float.Parse(textBoxTotalPrice.Text); }
        }

        public string Description
        {
            get { return textBoxDescription.Text; }
        }

        public string PictureProduct
        {
            get { return openFile.SafeFileName; }
        }

        public string Message
        {
            get
            {
                return null;
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        public int CodeProduct
        {
            get
            {
                return int.Parse(textBoxCodeProduct.Text);
            }
            set
            {
                textBoxCodeProduct.Text = Convert.ToString(value);
            }
        }

        public int StockCurrent
        {
            get
            {
                return int.Parse(textBoxStockCurrent.Text);
            }
            set
            {
                textBoxStockCurrent.Text = Convert.ToString(value);
            }
        }
    }
}
